import { CustomTableQuestion, InputQuestion, QuestionBase, RadioQuestion, TableBase, TextQuestion, TextTable, TimeQuestion } from '@/components/public-template';
import dayjs from 'dayjs';
import { formStoreWithOut } from '@/store/modules/form';
import { regExp } from '@/utils/regExp';

const formStore = formStoreWithOut();

const dictionaryArr = (dictionary) => {
  let arr = [];
  for (let i in formStore.getDictionary[dictionary]) {
    arr.push({ label: formStore.getDictionary[dictionary][i], value: Number(i) });
  }
  return arr;
};

class IndexService {
  // 基础信息
  getBasicQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'billCode',
        label: '单据编号'
      }),
      new TextQuestion({
        key: 'creator',
        label: '创建人ID',
        hide: true
      }),
      new TextQuestion({
        key: 'creatorName',
        label: '创建人'
      }),
      new TextQuestion({
        key: 'department',
        label: '创建部门ID',
        hide: true
      }),
      new TextQuestion({
        key: 'departmentName',
        label: '创建部门'
      }),
      new TextQuestion({
        key: 'createdTime',
        label: '创建时间'
      })
    ];
    return questions;
  }

  // 借阅档案列表(申请)
  getBorrowingListAddQuestions({ showType }) {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'customTable',
        value: [],
        state: {
          grid: 24,
          fullLine: true,
          isAdd: true,
          scroll: { x: 1500 },
          columns: [
            {
              title: '序号',
              type: 'index'
            },
            {
              title: '文件名称',
              required: true,
              prompt: '请选择',
              key: 'name',
              type: 'select',
              state: {
                optionFilterProp: 'label'
              }
            },
            {
              title: '档案编码',
              key: 'filesBoxCode'
            },
            {
              title: '借阅类型',
              key: 'borrowType',
              required: true,
              prompt: '请选择',
              type: 'select',
              state: {
                filterName: 'DMS_ARCHIVES_FILE_BORROW_BORROW_TYPE' // 1=纸质版原件，2=电子件，3=声像文件，4=实物档案
              }
            },
            {
              title: '文件号',
              key: 'fileCode'
            },
            {
              title: '档案类型',
              key: 'filesTypeName'
            },
            {
              title: '保密等级',
              key: 'secretLevel',
              type: 'dictionary',
              state: {
                filterName: 'SECRET_LEVEL'
              }
            },
            {
              title: '是否下载',
              key: 'isDownoad',
              // required: true,
              disabled: [{ key: 'borrowType', value: [1, 4] }],
              type: 'select',
              options: dictionaryArr('DMS_ARCHIVES_FILE_BORROW_IS_DOWNLOAD')
            },
            {
              title: '外借形式',
              key: 'borrowForm',
              // required: true,
              disabled: [{ key: 'borrowType', value: [2, 3] }],
              type: 'select',
              options: dictionaryArr('DMS_ARCHIVES_FILE_BORROW_BORROW_FORM') // 1=外借，2=现场查阅
            },
            {
              title: '文件编码',
              key: 'code'
            },
            // 非申请显示该字段
            showType !== 'ADD'
              ? {
                  title: '借阅编码',
                  key: 'borrowCode'
                }
              : {},
            {
              title: '操作',
              type: 'operation',
              handle: [
                {
                  type: 'delete',
                  text: '删除',
                  class: 'text-primary',
                  url: '/url',
                  params: [{ id: 'id' }],
                  state: {}
                }
              ]
            }
          ]
        }
      })
    ];
    return questions;
  }

  // 借阅档案列表(详情)
  getBorrowingListTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        value: '序号',
        type: 'index'
      }),
      new TextTable({
        key: 'borrowType',
        value: '借阅类型',
        type: 'dictionary',
        state: {
          filterName: 'DMS_ARCHIVES_FILE_BORROW_BORROW_TYPE' // 1=纸质版原件，2=电子件，3=声像文件，4=实物档案
        }
      }),
      new TextTable({
        key: ['fileListList', 'name'],
        value: '文件名称'
      }),
      new TextTable({
        key: ['fileListList', 'filesBoxCode'],
        value: '档案编码'
      }),
      new TextTable({
        key: ['fileListList', 'fileCode'],
        value: '文件号'
      }),
      new TextTable({
        key: ['fileListList', 'filesTypeName'],
        value: '档案类型'
      }),
      new TextTable({
        key: ['fileListList', 'secretLevel'],
        value: '保密等级',
        type: 'dictionary',
        state: {
          filterName: 'SECRET_LEVEL'
        }
      }),
      new TextTable({
        key: 'isDownoad',
        value: '是否下载',
        type: 'dictionary',
        state: {
          filterName: 'DMS_ARCHIVES_FILE_BORROW_IS_DOWNLOAD'
        }
      }),
      new TextTable({
        key: 'borrowForm',
        value: '外借形式',
        type: 'dictionary',
        state: {
          filterName: 'DMS_ARCHIVES_FILE_BORROW_BORROW_FORM'
        }
      }),
      new TextTable({
        key: ['fileListList', 'code'],
        value: '文件编码'
      }),
      new TextTable({
        key: 'borrowCode',
        value: '借阅编码'
      }),
      new TextTable({
        key: 'actions',
        value: '操作',
        handle: [
          {
            type: 'local',
            text: '预览',
            class: 'text-primary',
            state: {}
          },
          {
            type: 'local',
            text: '下载',
            class: 'text-primary',
            state: {},
            showKey: [{ key: 'isDownoad', value: 1 }]
          }
        ]
      })
    ];
    return tables;
  }

  // 借阅信息（申请）
  getBorrowingAddQuestions() {
    const questions: QuestionBase<any>[] = [
      new TimeQuestion({
        key: 'borrowTime',
        label: '借阅时间',
        required: true,
        type: 'alone',
        prompt: '请选择',
        value: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        state: {
          showTime: true,
          format: 'YYYY-MM-DD HH:mm:ss',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        hide: true
      }),
      new InputQuestion({
        key: 'borrowDuration',
        label: '借阅时长/天',
        required: true,
        prompt: '请输入',
        type: 'slot',
        state: {
          slotName: 'borrowDuration'
        }
      }),
      new InputQuestion({
        key: 'borrowReason',
        label: '借阅事由',
        required: true,
        reg: regExp.len100,
        placeholder: '请输入',
        prompt: '请输入, 字段长度至多不超过100'
      }),
      new InputQuestion({
        key: 'remark',
        label: '备注',
        type: 'textarea',
        reg: regExp.len100,
        prompt: '长度不可超过100个字符'
      }),
      new InputQuestion({
        key: 'expirationReminder',
        label: '到期提醒时间',
        type: 'slot',
        state: {
          slotName: 'expirationReminder'
        }
      })
    ];
    return questions;
  }

  // 借阅信息（详情）
  getBorrowingViewQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'borrowTime',
        label: '借阅时间'
      }),
      new TextQuestion({
        key: 'borrowDuration',
        label: '借阅时长/天'
      }),
      new TextQuestion({
        key: 'borrowReason',
        label: '借阅事由',
        type: 'textarea'
      }),
      new TextQuestion({
        key: 'expirationReminder',
        label: '到期提醒时间'
      }),
      new TextQuestion({
        key: 'planReturnTime',
        label: '计划归还时间'
      }),
      new TextQuestion({
        key: 'remark',
        label: '备注'
      })
    ];
    return questions;
  }

  // 审批意见
  getApprovalOpinionsQuestions() {
    const questions: QuestionBase<any>[] = [
      new RadioQuestion({
        key: 'applyResult',
        label: '意见',
        required: true,
        prompt: '请选择',
        value: 1,
        options: [
          {
            label: '通过',
            value: 1
          },
          {
            label: '驳回',
            value: 2
          }
        ]
      }),
      new InputQuestion({
        key: 'remark',
        label: '备注',
        type: 'textarea',
        reg: regExp.len100,
        prompt: '长度不可超过100个字符'
      })
    ];
    return questions;
  }
}

export default new IndexService();
